package cn.itsource.pethome.annotation.impl;

import cn.itsource.pethome.annotation.IPermissionScanService;
import cn.itsource.pethome.annotation.PreAuthorize;
import cn.itsource.pethome.system.domain.Permission;
import cn.itsource.pethome.system.mapper.PermissionMapper;
import cn.itsource.pethome.util.Classutil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.FileFilter;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;

@Service
public class PermissionScanServiceImpl implements IPermissionScanService {
    @Autowired
    private PermissionMapper permissionMapper;

    private static final String  PKG_PREFIX   = "cn.itsource.pethome.";
    private static final String  PKG_SUFFIX   = ".controller";

    @Override
    public void scanPermission() {
        //真正代码的实现
        //找到包        classes
        String path = this.getClass().getResource("/").getPath() + "/cn/itsource/pethome/";
        File file = new File(path);
        File[] files = file.listFiles(new FileFilter() {//这里面是文件夹
            @Override
            public boolean accept(File file) {
                return file.isDirectory();
            }
        });
        //获取org.yaosang.*.web.controller里面所有的类
        Set<Class> clazzes = new HashSet<>();//装类的
        for (File fileTmp : files) {
            //System.out.println(fileTmp.getName()); //文件夹的名字

            //将controller里面的所有class全部读取出来并且保存到set集合
            //                                  cn.itsource.pethome.xxx.controller
            clazzes.addAll(Classutil.getClasses(PKG_PREFIX+fileTmp.getName()+PKG_SUFFIX));
        }
        //找到类
        for (Class clazz : clazzes) {//这里全是controller类
            //通过反射获取所有的方法
            Method[] methods = clazz.getMethods();
            if(methods == null){
                continue; //为什么   跳过本次循环
            }
            //找到方法
            for (Method method : methods) {
                String url = getUri(clazz, method);
                //找到注解
                PreAuthorize annotation = method.getAnnotation(PreAuthorize.class);
                if(annotation == null){
                    continue;//跳过本次循环
                }
                //解析注解
                String name = annotation.name();
                String sn = annotation.value();
                //校验是否已经存该权限
                Permission permission = permissionMapper.queryOneBySn(sn);
                if(permission != null){
                    continue;
                }
                permission = new Permission();
                permission.setName(name);
                permission.setSn(sn);
                permission.setUrl(url);//获取到路径
                //保存数据库
                permissionMapper.insert(permission);
            }
        }

    }


    private String getUri(Class clazz, Method method) {
        String classPath = "";
        Annotation annotation = clazz.getAnnotation(RequestMapping.class);
        if (annotation!=null){
            RequestMapping requestMapping = (RequestMapping) annotation;
            String[] values = requestMapping.value();
            if(values!=null&&values.length>0){
                classPath = values[0];
                if (!"".equals(classPath)&&!classPath.startsWith("/"))
                    classPath="/"+classPath;
            }
        }
        GetMapping getMapping =  method.getAnnotation(GetMapping.class);
        String methodPath = "";
        if (getMapping!=null){
            String[] values = getMapping.value();
            if(values!=null&&values.length>0){
                methodPath = values[0];
                if (!"".equals(methodPath)&&!methodPath.startsWith("/"))
                    methodPath="/"+methodPath;
            }
        }
        PostMapping postMapping =  method.getAnnotation(PostMapping.class);
        if (postMapping!=null){
            String[] values = postMapping.value();
            if(values!=null&&values.length>0){
                methodPath = values[0];
                if (!"".equals(methodPath)&&!methodPath.startsWith("/"))
                    methodPath="/"+methodPath;
            }
        }
        DeleteMapping deleteMapping =  method.getAnnotation(DeleteMapping.class);
        if (deleteMapping!=null){
            String[] values = deleteMapping.value();
            if(values!=null&&values.length>0){
                methodPath = values[0];
                if (!"".equals(methodPath)&&!methodPath.startsWith("/"))
                    methodPath="/"+methodPath;
            }
        }
        PutMapping putMapping =  method.getAnnotation(PutMapping.class);
        if (putMapping!=null){
            String[] values = putMapping.value();
            if(values!=null&&values.length>0){
                methodPath = values[0];
                if (!"".equals(methodPath)&&!methodPath.startsWith("/"))
                    methodPath="/"+methodPath;
            }

        }
        PatchMapping patchMapping = method.getAnnotation(PatchMapping.class);
        if (patchMapping!=null){
            String[] values = patchMapping.value();
            if(values!=null&&values.length>0){
                methodPath = values[0];
                if (!"".equals(methodPath)&&!methodPath.startsWith("/"))
                    methodPath="/"+methodPath;
            }
        }
        RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
        if (requestMapping!=null){
            String[] values = requestMapping.value();
            if(values!=null&&values.length>0){
                methodPath = values[0];
                if (!"".equals(methodPath)&&!methodPath.startsWith("/"))
                    methodPath="/"+methodPath;
            }
        }
        return classPath+methodPath;
    }
}
